home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 23 / AMIGAplus Sonderheft 23 (2000)(Falke)(DE)[!].iso / Tools / Text-Viewer / MSWordView / mswordview_src / support.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-06  |  23.9 KB  |  998 lines

  1. /*
  2. Released under GPL, written by Caolan.McNamara@ul.ie.
  3.  
  4. Copyright (C) 1998 
  5.     Caolan McNamara
  6.  
  7. Real Life: Caolan McNamara           *  Doing: MSc in HCI
  8. Work: Caolan.McNamara@ul.ie          *  Phone: +353-61-202699
  9. URL: http://skynet.csn.ul.ie/~caolan *  Sig: an oblique strategy
  10. How would you have done it?
  11. */
  12.  
  13. /*warning: this software requires laola's lls to be installed*/
  14.  
  15. /*
  16.  
  17. this code is often all over the shop, being more of an organic entity
  18. that a carefully planed piece of code, so no laughing there at the back!
  19.  
  20. and send me patches by all means, but think carefully before sending me
  21. a patch that doesnt fix a bug or add a feature but instead just changes
  22. the style of coding, i.e no more thousand line patches that fix my 
  23. indentation.
  24.  
  25. */
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <ctype.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <math.h>
  33. #include "config.h"
  34. #include "mswordview.h"
  35. #include "roman.h"
  36. #include "utf.h"
  37.  
  38. extern char *symbolurl;
  39. extern char *wingdingurl;
  40. extern char *patternurl;
  41. extern FILE *outputfile;
  42. extern FILE *erroroutput;
  43. extern char *imagesurl;
  44. extern char *imagesdir;
  45. extern int nofontfaces;
  46. extern int incenter;
  47. extern int inrightjust;
  48. extern char backgroundcolor[8];
  49. extern long int cp;
  50. extern long int realcp;
  51.  
  52.  
  53. /*
  54. im only going to use fontfaces if the language is Ascii based, as there
  55. would only be grief working out fonts for eastern fonts the way the
  56. word system is at the moment
  57. */
  58. int use_fontfacequery(chp *achp)
  59.     {
  60.     if (nofontfaces)
  61.         return 0;
  62.     error(erroroutput,"fontface query %d %d %d %d\n",achp->eastfont,achp->idctHint,achp->ascii_font,achp->noneastfont);
  63.     if ((achp->eastfont == 0) && (achp->idctHint == 0) && (achp->ascii_font == achp->noneastfont))
  64.         return 1;
  65.     return 0;
  66.     }
  67.  
  68.  
  69. ffn *get_fontnamefromcode(ffn *fontnamelist,int fontcode, int *index)
  70.     {
  71.     ffn *tempfont;
  72.     *index=0;
  73.     tempfont = fontnamelist;
  74.     while (*index < fontcode)
  75.         {
  76.         tempfont = tempfont->next;
  77.         if (tempfont == NULL)
  78.             break;
  79.         (*index)++;
  80.         }
  81.     return(tempfont);
  82.     }
  83.  
  84. void decode_list_nfc(int *value,int no_type)
  85.     {
  86.     char roman[81];
  87.     switch(no_type)
  88.         {
  89.         case 0:
  90.             fprintf(outputfile,"%d",(*value)++);
  91.             break;
  92.         case 1:
  93.             fprintf(outputfile,"%s",decimalToRoman((*value)++,roman));
  94.             break;
  95.         case 2:
  96.             fprintf(outputfile,"%s",ms_strlower(decimalToRoman((*value)++,roman)));
  97.             break;
  98.         case 3:
  99.             fprintf(outputfile,"%c",64+(*value)++); /*uppercase letter*/
  100.             break;
  101.         case 4:
  102.             fprintf(outputfile,"%c",96+(*value)++); /*lowercase letter*/
  103.             break;
  104.         default:
  105.             break;
  106.         }
  107.     }
  108.  
  109. int decode_symbol(U16 fontspec)
  110.     {
  111.     error(erroroutput,"given symbol %ld, converting to %ld\n",fontspec,fontspec-61472);
  112.     fontspec = fontspec-61472;
  113.     if (fontspec < 95)
  114.         fontspec+=34;
  115.     fprintf(outputfile,"<img src=\"%s/%d.gif\">",symbolfontdir(),fontspec);
  116.     return(1);
  117.     }
  118.     
  119. char *symbolfontdir(void)
  120.     {
  121.     if (symbolurl != NULL)
  122.         return(symbolurl);
  123.     return(SYMBOLFONTDIR);
  124.     }
  125.  
  126. int decode_wingding(U16 fontspec)
  127.     {
  128.     error(erroroutput,"given wingding %ld, converting to %ld\n",fontspec,fontspec-61472);
  129.     fontspec = fontspec-61472;
  130.     fontspec+=34;
  131.     if (fontspec > 86) 
  132.         fontspec+=4;
  133.     if (fontspec == 114)
  134.         fontspec = 153;
  135.     else if (fontspec == 115)
  136.         fontspec = 160;
  137.     else if (fontspec == 116)
  138.         fontspec = 163;
  139.     else if (fontspec == 117)
  140.         fontspec = 165;
  141.     else if (fontspec == 118)
  142.         fontspec = 164;
  143.     else if (fontspec == 119)
  144.         fontspec = 168;
  145.     else if (fontspec == 120)
  146.         fontspec = 167;
  147.     else if (fontspec == 121)
  148.         fontspec = 171;
  149.     else if (fontspec == 122)
  150.         fontspec = 172;
  151.     else if (fontspec == 123)
  152.         fontspec = 170;
  153.     else if (fontspec == 124)
  154.         fontspec = 184;
  155.     else if (fontspec == 125)
  156.         fontspec = 169;
  157.     else if (fontspec == 129)
  158.         fontspec = 116;
  159.     else if (fontspec == 130)
  160.         fontspec = 117;
  161.     else if (fontspec == 131)
  162.         fontspec = 114;
  163.     else if (fontspec == 132)
  164.         fontspec = 115;
  165.     else if ( (fontspec > 132) && (fontspec < 156) )
  166.         fontspec-=5;
  167.     else if ( (fontspec >= 156) && (fontspec < 164 ) )
  168.         fontspec-=38;
  169.     else if (fontspec == 164)
  170.         fontspec=151;
  171.     else if (fontspec == 165)
  172.         fontspec=152;
  173.     else if (fontspec == 166)
  174.         fontspec=161;
  175.     else if  ((fontspec > 166) && (fontspec < 173 ) )
  176.         fontspec-=13;
  177.     else if (fontspec == 173)
  178.         fontspec=162;
  179.     else if (fontspec == 174)
  180.         fontspec=164;
  181.     else if ((fontspec > 174) && (fontspec < 186))
  182.         fontspec-=2;
  183.     else if ( (fontspec >= 186) && (fontspec < 257) )
  184.         fontspec--;
  185.     else if (fontspec > 256)
  186.         fontspec-=170;
  187.     
  188.     fprintf(outputfile,"<img src=\"%s/%d.gif\">",wingdingfontdir(),fontspec);
  189.     return(1);
  190.     }
  191.  
  192. char *patterndir(void)
  193.     {
  194.     if (patternurl != NULL)
  195.         return(patternurl);
  196.     return(PATTERNDIR);
  197.     }
  198.     
  199. char *wingdingfontdir(void)
  200.     {
  201.     if (wingdingurl != NULL)
  202.         return(wingdingurl);
  203.     return(WINGDINGFONTDIR);
  204.     }
  205.  
  206.  
  207. char *ms_basename(char *filename)
  208.     {
  209.     char *temppointer=NULL;
  210.     if ((filename != NULL) && (filename[0] != '\0'))
  211.         {
  212.         temppointer = filename+strlen(filename);
  213. #ifndef WINDOWS
  214.         while ((temppointer != filename) && (*(temppointer-1) != '/'))
  215.             temppointer--;
  216. #else
  217.         while ((temppointer != filename) && (*(temppointer-1) != '\\'))
  218.             temppointer--;
  219. #endif
  220.         }
  221.     return temppointer;
  222.     }
  223.  
  224. void outputimgsrc(char *filename)
  225.     {
  226.     char *temppointer;
  227.  
  228.  
  229.     if (filename!= NULL)
  230.         {
  231.         temppointer = ms_basename(filename);
  232.         if ((imagesurl == NULL) && (imagesdir == NULL))
  233.             fprintf(outputfile,"<img src=\"%s\"><br>",temppointer);
  234.         else if ( (imagesurl == NULL) && (imagesdir != NULL) )
  235.             fprintf(outputfile,"<img src=\"%s/%s\"><br>",imagesdir,temppointer);
  236.         else
  237.             fprintf(outputfile,"<img src=\"%s/%s\"><br>",imagesurl,temppointer);
  238.         error(erroroutput,"success!\n");
  239.         }
  240.     else
  241.         error(erroroutput,"filename was null!\n");
  242.     }
  243.  
  244.  
  245. char *ms_strlower(char *in)
  246.     {
  247.     char *useme = in;
  248.     while(*useme != '\0')
  249.         {
  250.         *useme = tolower(*useme);
  251.         useme++;
  252.         }
  253.     return(in);
  254.     }
  255.  
  256.  
  257. U32 read_32ubit(FILE *in)
  258.     {
  259.     U16 temp1,temp2;
  260.     U32 ret;
  261.     temp1 = read_16ubit(in);
  262.     temp2 = read_16ubit(in); 
  263.     ret = temp2;
  264.     ret = ret << 16;
  265.     ret += temp1;
  266.     return(ret);
  267.     }
  268.  
  269. U32 sread_32ubit(U8 *in)
  270.     {
  271.     U16 temp1,temp2;
  272.     U32 ret;
  273.     temp1 = sread_16ubit(in);
  274.     temp2 = sread_16ubit(in+2);
  275.     ret = temp2;
  276.     ret = ret << 16;
  277.     ret+=temp1;
  278.     return(ret);
  279.     }
  280.  
  281. U16 read_16ubit(FILE *in)
  282.     {
  283.     /*im pretty insecure when it come to dealing with bits, i like
  284.      *to address them by their meaning not the reality
  285.      */
  286.     U8 temp1,temp2;
  287.     U16 ret;
  288.     temp1 = getc(in);
  289.     temp2 = getc(in);
  290.     ret = temp2;
  291.     ret = ret << 8;
  292.     ret += temp1;
  293.     return(ret);
  294.     }
  295.  
  296.  
  297. U16 sread_16ubit(U8 *in)
  298.     {
  299.     U8 temp1,temp2;
  300.     U16 ret;
  301.     temp1 = *in;
  302.     temp2 = *(in+1);
  303.     ret = temp2;
  304.     ret = ret << 8;
  305.     ret += temp1;
  306.     return(ret);
  307.     }
  308.  
  309. U32 dread_32ubit(FILE *in,U8 **list)
  310.     {
  311.     U8 *temp;
  312.     U32 ret;
  313.     if (list == NULL)
  314.         return(read_32ubit(in));
  315.     else
  316.         {
  317.         temp = *list;
  318.         (*list)+=4;
  319.         ret = sread_32ubit(temp);
  320.         return(ret);
  321.         }
  322.     }
  323.  
  324. U16 dread_16ubit(FILE *in,U8 **list)
  325.     {
  326.     U8 *temp;
  327.     U16 ret;
  328.     if (list == NULL)
  329.         return(read_16ubit(in));
  330.     else
  331.         {
  332.         temp = *list;
  333.         (*list)+=2;
  334.         ret = sread_16ubit(temp);
  335.         return(ret);
  336.         }
  337.     }
  338.  
  339. U8 dgetc(FILE *in,U8 **list)
  340.     {
  341.     U8 *temp;
  342.     if (list == NULL)
  343.         return(getc(in));
  344.     else
  345.         {
  346.         temp = *list;
  347.         (*list)++;
  348.         return(sgetc(temp));
  349.         }
  350.     }
  351.  
  352. U8 sgetc(U8 *in)
  353.     {
  354.     return(*in);
  355.     }
  356.  
  357.  
  358. int isodd(int i)
  359.     {
  360.     if ( (i/2) == ((i+1)/2) )
  361.         return 0;
  362.     return 1;
  363.     }
  364.  
  365. void oprintf(int silentflag,char *fmt, ...)
  366.     {
  367.     va_list argp;
  368.     if (!silentflag)
  369.         {
  370.         va_start(argp, fmt);
  371.         vfprintf(outputfile, fmt, argp);
  372.         va_end(argp);
  373.         }
  374.     }
  375.  
  376. void error(FILE *stream,char *fmt, ...)
  377.     {
  378.     #ifdef DEBUG
  379.     va_list argp;
  380.     fprintf(stream, "error: ");
  381.     va_start(argp, fmt);
  382.     vfprintf(stream, fmt, argp);
  383.     va_end(argp);
  384.     fprintf(stream, "\n");
  385.     fflush(stream);
  386.     #endif
  387.     }
  388.  
  389. RETSIGTYPE reaper (int ignored)
  390.     {
  391. #ifdef MUST_REINSTALL_SIGHANDLERS
  392.     signal_handle (SIGCHLD, reaper);
  393. #endif
  394.     while (WAITPID (-1, 0, WNOHANG) > 0)
  395.         ;
  396.  
  397.     }
  398.  
  399. RETSIGTYPE timeingout(int ignored)
  400.     {
  401.     fprintf(erroroutput,"\nconversion took too long, assuming something wrong and aborting\n");
  402.     fprintf(erroroutput,"\nset timeout value higher (or dont set it) to try for longer\n");
  403.     exit(-1);
  404.     }
  405.  
  406. #if defined (HAVE_POSIX_SIGNALS)
  407. void signal_handle (int sig, SigHandler * handler)
  408.     {
  409.     struct sigaction act, oact;
  410.  
  411.     act.sa_handler = handler;
  412.     act.sa_flags = 0 | SA_NOCLDSTOP | SA_RESTART;
  413.     sigemptyset (&act.sa_mask);
  414.     sigemptyset (&oact.sa_mask);
  415.     sigaction (sig, &act, &oact);
  416.     }
  417. #endif
  418.  
  419. olestream * divide_streams(char *filename,char **analyze,char **slashtmp,  char *argv0)
  420.     {
  421.     olestream *olelist;
  422.     olestream *olelistptr;
  423.     pid_t pid;
  424.     int filedes[2];
  425.     char *execut[4];
  426.     char buffer[4096];
  427.     char fullfilepath[PATH_MAX];
  428.     char currentdir[PATH_MAX];
  429.     int i,j,k=0;
  430.     int scan=0;
  431.     int scanbracket=0;
  432.     int scanlevel=0;
  433.     int level=0;
  434.     int len;
  435.     
  436.     char numbers[4];
  437.     
  438.     char *ptr=NULL;
  439.     char basename[PATH_MAX];
  440.  
  441.     memset(fullfilepath, 0, PATH_MAX);
  442.     memset(currentdir, 0, PATH_MAX);
  443.     memset(basename, 0, PATH_MAX);
  444.  
  445.     execut[0] = "lls-mswordview";
  446.     execut[1] = "-s";
  447.     execut[3] = NULL;
  448.  
  449.  
  450.     if (pipe (filedes))
  451.         {
  452.         fprintf (erroroutput, "Pipe failed.\n");
  453.         exit(-1);
  454.         }
  455.  
  456.     if (filename[0] != '/')
  457.         {
  458.         if (NULL == getcwd(currentdir,PATH_MAX))
  459.             {
  460.             fprintf (erroroutput, "couldnt get current directory\n");
  461.             exit(-1);
  462.             }
  463.         strcpy(fullfilepath,currentdir);
  464.         }
  465.     if (currentdir[strlen(currentdir)] != '/')
  466.         strcat(currentdir,"/");
  467.     if (fullfilepath[strlen(fullfilepath)] != '/')
  468.         strcat(fullfilepath,"/");
  469.     strncat(fullfilepath,filename,PATH_MAX-(strlen(fullfilepath)+1));
  470.     error(erroroutput,"full filename is %s\n",fullfilepath);
  471.  
  472.     execut[2] = fullfilepath;
  473.  
  474.     *slashtmp = tmpnam(NULL);
  475.     if (0 != mkdir((const char *)*slashtmp,S_IRWXU))
  476.         {
  477.         fprintf (erroroutput, "mkdir of %s failed\n",*slashtmp);
  478.         exit(-1);
  479.         }
  480.     else
  481.         {
  482.         error(erroroutput, "mkdir of %s suceeded\n",*slashtmp);
  483.         }
  484.  
  485.     *analyze =(char *) malloc (strlen(*slashtmp) + strlen("analyze") + 3);
  486.     if (*analyze == NULL)
  487.         {
  488.         fprintf (erroroutput, "malloc failed\n");
  489.         exit (-1);
  490.         }
  491.     strcpy(*analyze,*slashtmp);
  492.     strcat(*analyze,"/analyze/");
  493.  
  494.     strcpy(basename,*analyze); 
  495.  
  496.     pid = fork ();
  497.     if (pid == (pid_t) 0)  /*child*/
  498.         {
  499.         close (1);                      /* close stdout */
  500.         dup (filedes[1]);               /* make pipe stdout */
  501.         close (filedes[1]);
  502.         chdir(*slashtmp);
  503.         execvp(execut[0],(char* const *) execut);
  504. #if defined(HAVE_ERRNO_H)
  505.         fprintf( erroroutput, "Exec of %s failed with error code %d, trying a different tack\n", execut[0], errno );
  506. #else
  507.         fprintf( erroroutput, "Exec of %s failed, trying a different tack\n", execut[0]);
  508. #endif
  509.         /*if that failed try again, with zack@studioarchetype.com idea*/
  510.         if (strchr(argv0,'/') != NULL) 
  511.             {
  512.             execut[0] = malloc((strlen(argv0) + strlen("/lls-mswordview") + 1) * sizeof(char));
  513.             argv0[strlen(argv0) - strlen(strrchr(argv0,'/'))] = '\0';
  514.             sprintf(execut[0],"%s/lls-mswordview",argv0);
  515.             } 
  516.         else 
  517.             {
  518.             execut[0] = malloc((strlen(currentdir) + strlen("/lls-mswordview") + 1) * sizeof(char));
  519.             sprintf(execut[0],"%s/lls-mswordview",currentdir);
  520.             }
  521.         execvp(execut[0],(char* const *) execut);
  522. #if defined(HAVE_ERRNO_H)
  523.         fprintf( erroroutput, "Exec of %s also failed with error code %d, trying one last tack\n", execut[0], errno );
  524. #else
  525.         fprintf( erroroutput, "Exec of %s also failed, trying one last tack\n", execut[0]);
  526. #endif
  527.         /*if that failed try again, with zack@studioarchetype.com idea*/
  528.         if (strchr(argv0,'/') != NULL) 
  529.             {
  530.             execut[0] = malloc((strlen(argv0) + strlen("/laola/lls-mswordview") + 1) * sizeof(char));
  531.             argv0[strlen(argv0) - strlen(strrchr(argv0,'/'))] = '\0';
  532.             sprintf(execut[0],"%s/laola/lls-mswordview",argv0);
  533.             } 
  534.         else 
  535.             {
  536.             execut[0] = malloc((strlen(currentdir) + strlen("/laola/lls-mswordview") + 1) * sizeof(char));
  537.             sprintf(execut[0],"%s/laola/lls-mswordview",currentdir);
  538.             }
  539.         execvp(execut[0],(char* const *) execut);
  540. #if defined(HAVE_ERRNO_H)
  541.         fprintf( erroroutput, "Exec of %s also failed with error code %d\n", execut[0], errno );
  542. #else
  543.         fprintf( erroroutput, "Exec of %s also failed\n", execut[0]);
  544. #endif
  545.         exit(-1);
  546.         }
  547.     else if (pid < (pid_t) 0)
  548.         {
  549.         /* The fork failed. */
  550.         fprintf (erroroutput, "Fork failed.\n");
  551.         exit (-1);
  552.         }
  553.  
  554.     close (filedes[1]);
  555.  
  556.  
  557.     olelist = (olestream *) malloc(sizeof(olestream));
  558.     if (olelist == NULL)
  559.         {
  560.         fprintf (erroroutput, "malloc failed.\n");
  561.         exit (-1);
  562.         }
  563.  
  564.  
  565.     /*work backwards to the the first / or beginning*/
  566.  
  567.     k = strlen(basename);
  568.  
  569.     j=strlen(filename);
  570.     while((filename[j] != '/') && (j!=0))
  571.         j--;
  572.  
  573.     /*j now a beginning of basename*/
  574.     while((filename[j] != '.') && (j<strlen(filename)))
  575.         {
  576.         basename[k++] = filename[j++];
  577.         basename[k] = '\0';
  578.         }
  579.  
  580.     basename[k++] = '.';
  581.     basename[k] = '\0';
  582.     error(erroroutput,"basename is %s\n",basename);
  583.  
  584.     error(erroroutput,"filename base is %s\n",basename);
  585.  
  586.     olelistptr = olelist;
  587.  
  588.  
  589.     while ((len = read (filedes[0],buffer,4095)) > 0)
  590.         {
  591.         for (i=0;i<len;i++)
  592.             {
  593.             if ((buffer[i] == '\'') && (scan == 0))
  594.                 {
  595.                 scan=1;
  596.                 ptr = olelistptr->streamname;
  597.                 j=0;
  598.                 }
  599.             else if ((buffer[i] == '\'') && (scan == 1))
  600.                 {
  601.                 scan=0;
  602.                 ptr[j] = '\0';
  603.                 }
  604.             else if ((buffer[i] == '(') && (scanbracket == 0))
  605.                 {
  606.                 error(erroroutput,"streamname is %s\n",olelistptr->streamname);
  607.                 scanbracket=1;
  608.                 ptr = numbers;
  609.                 j = 0;
  610.                 }
  611.             else if ((buffer[i] == ')') && (scanbracket == 1))
  612.                 {
  613.                 scanbracket=0;
  614.                 ptr[j] = '\0';
  615.                 error(erroroutput,"filename nos %s\n",numbers);
  616.                 sprintf(olelistptr->filename,"%s%.2x",basename,(int)strtol(numbers,(char **)NULL,16));
  617.                 error(erroroutput,"filename is %s\n",olelistptr->filename);
  618.                 olelistptr->level = level/3;
  619.                 error(erroroutput,"level is %d\n",level/3);
  620.                 olelistptr->next = (olestream *) malloc(sizeof(olestream));
  621.                 if (olelistptr == NULL)
  622.                     {
  623.                     fprintf (erroroutput, "malloc failed.\n");
  624.                     exit (-1);
  625.                     }
  626.                 olelistptr = olelistptr->next;
  627.                 olelistptr->next=NULL;
  628.                 }
  629.             else if (scan == 1)
  630.                 {
  631.                 ptr[j++] = buffer[i];
  632.                 }
  633.             else if (scanbracket==1)
  634.                 {
  635.                 if (isxdigit(buffer[i]))
  636.                     ptr[j++] = buffer[i];
  637.                 }
  638.             else if (buffer[i] == ':')
  639.                 {
  640.                 scanlevel=1;
  641.                 level=0;
  642.                 }
  643.             else if (scanlevel == 1)
  644.                 {
  645.                 if (isspace(buffer[i]))
  646.                     level++;
  647.                 else
  648.                     scanlevel=0;
  649.                 }
  650.             }
  651.         }
  652.  
  653.     return(olelist);
  654.     }
  655.  
  656. void cleanupstreams(char *analyze,char *slashtmp)
  657.     {
  658.     DIR *adir;
  659.     struct dirent *thedir;
  660.     char currentdir[PATH_MAX] ="/";
  661.  
  662.      if (NULL == getcwd(currentdir,PATH_MAX))
  663.             fprintf (erroroutput, "minor warning: couldnt get current directory path,continuing.\n");
  664.  
  665.     chdir(analyze);
  666.     adir = opendir(analyze);    
  667.     if (adir == NULL)
  668.         error (erroroutput, "couldnt open %s\n",analyze);
  669.     else
  670.         {
  671.         while (NULL != (thedir = readdir(adir)))
  672.             {
  673.             if (strcmp(thedir->d_name,"..") && strcmp(thedir->d_name,"."))
  674.                 {
  675.                 error(erroroutput,"removing %s\n",thedir->d_name);
  676.                 remove(thedir->d_name);
  677.                 }
  678.             }
  679.         closedir(adir);
  680.         }
  681.     chdir(currentdir);
  682.  
  683.     if (0 != rmdir((const char *)analyze))
  684.         error(erroroutput, "rmdir of %s failed\n",analyze);
  685.     else
  686.         error(erroroutput, "rmdir of %s suceeded\n",analyze);
  687.  
  688.     if (0 != rmdir((const char *)slashtmp))
  689.         error(erroroutput, "rmdir of %s failed\n",slashtmp);
  690.     else
  691.         error (erroroutput, "rmdir of %s suceeded\n",slashtmp);
  692.     }
  693.  
  694. int setdecom(void)
  695.     {
  696. #ifdef SYSTEM_ZLIB
  697.     return(1);
  698. #endif
  699.     fprintf(erroroutput,"Warning: mswordview was not compiled against zlib, so wmf files cannot be\ndecompressed\n");
  700.     return(0);
  701.     }
  702.  
  703. void sectionbreak(sep *asep)
  704.     {
  705.     /*
  706.     i may need to put code here to add 1 to a number based
  707.     on the even odd section breaks, or maybe that only affects
  708.     what side of a book the page appears ?
  709.     */
  710.     if (incenter)
  711.         fprintf(outputfile,"</center>");
  712.     if (inrightjust)
  713.         fprintf(outputfile,"</div>");
  714.     if (asep != NULL)
  715.         {
  716.         switch (asep->bkc)
  717.             {
  718.             case 0:
  719.                 fprintf(outputfile,"\n<br><img src=\"%s/sectionendcontinous.gif\"><br>\n",patterndir());
  720.                 break;
  721.             case 1:
  722.                 fprintf(outputfile,"\n<br><img src=\"%s/sectionendcolumn.gif\"><br>\n",patterndir());
  723.                 break;
  724.             case 2:
  725.                 fprintf(outputfile,"\n<br><img src=\"%s/sectionendnewpage.gif\"><br>\n",patterndir());
  726.                 break;
  727.             case 3:
  728.                 fprintf(outputfile,"\n<br><img src=\"%s/sectionendeven.gif\"><br>\n",patterndir());
  729.                 break;
  730.             case 4:
  731.                 fprintf(outputfile,"\n<br><img src=\"%s/sectionendodd.gif\"><br>\n",patterndir());
  732.                 break;
  733.             }
  734.         }
  735.     else
  736.         error(erroroutput,"agh, null sep\n");
  737.     if (incenter)
  738.         fprintf(outputfile,"<center>");
  739.     if (inrightjust)
  740.         fprintf(outputfile,"<div>");
  741.     }
  742.  
  743. void pagebreak(void)
  744.     {
  745.     if (incenter)
  746.         fprintf(outputfile,"</center>");
  747.     if (inrightjust)
  748.         fprintf(outputfile,"</div>");
  749.     fprintf(outputfile,"\n<br><img src=\"%s/pagebreak.gif\"><br>\n",patterndir());
  750.     if (incenter)
  751.         fprintf(outputfile,"<center>");
  752.     if (inrightjust)
  753.         fprintf(outputfile,"<div>");
  754.     }
  755.  
  756. void columnbreak(void)
  757.     {
  758.     if (incenter)
  759.         fprintf(outputfile,"</center>");
  760.     if (inrightjust)
  761.         fprintf(outputfile,"</div>");
  762.     fprintf(outputfile,"\n<br><img src=\"%s/columnbreak.gif\"><br>\n",patterndir());
  763.     if (incenter)
  764.         fprintf(outputfile,"<center>");
  765.     if (inrightjust)
  766.         fprintf(outputfile,"<div>");
  767.     }
  768.  
  769. void check_auto_color(chp *achp)
  770.     {
  771.     /*
  772.     if the foreground color is auto (black basically) then see if the bg is a conflicting
  773.     color
  774.     */
  775.     if ( (!strcmp(backgroundcolor,"#000000")) && ( (achp->color[0] == '\0') || (!(strcmp(achp->color,"#000000"))))    )
  776.         {
  777.         error(erroroutput,"black on black\n");
  778.         strcpy(achp->color,"#ffffff");
  779.         }
  780.     else if ( (!strcmp(backgroundcolor,"#000078")) && ( (achp->color[0] == '\0') || (!(strcmp(achp->color,"#000000"))))     )
  781.         {
  782.         error(erroroutput,"black on blue\n");
  783.         strcpy(achp->color,"#ffffff");
  784.         }
  785.     }
  786.  
  787. void extract_sttbf(STTBF *bookmarks,FILE *tablefd,U32 fcSttbf,U32 lcbSttbf)
  788.     {
  789.     /*extract_sttbf(portions->bookmarks,tablefd,fcSttbfbkmk,lcbSttbfbkmk);*/
  790.     int i,j;
  791.     U16 temp,charlen;
  792.  
  793.     bookmarks->extra_bytes=NULL;
  794.     bookmarks->chars = NULL;
  795.     bookmarks->no_of_strings=0;
  796.  
  797.     if (lcbSttbf>0)
  798.         {
  799.         fseek(tablefd,fcSttbf,SEEK_SET);
  800.         error(erroroutput,"sttbf strinf offset is %x\n",fcSttbf);
  801.         temp = read_16ubit(tablefd);
  802.         if (temp == 0xFFFF)
  803.             {
  804.             bookmarks->exflag = temp;
  805.             bookmarks->no_of_strings = read_16ubit(tablefd);
  806.             }
  807.         else
  808.             {
  809.             bookmarks->exflag =0;
  810.             bookmarks->no_of_strings = temp;
  811.             }
  812.         bookmarks->extra_byte_flag = read_16ubit(tablefd);
  813.         error(erroroutput,"there are %d strings\n",bookmarks->no_of_strings);
  814.  
  815.         bookmarks->chars = (U16 **) malloc(bookmarks->no_of_strings*sizeof(U16*));
  816.         bookmarks->extra_bytes = (U8 **) malloc (bookmarks->no_of_strings*sizeof(U8*));
  817.         if ((bookmarks->chars == NULL) || (bookmarks->extra_bytes == NULL))
  818.             {
  819.             fprintf(erroroutput,"mem problem in bookmarks\n");
  820.             exit(-1);
  821.             }
  822.  
  823.         for (i=0;i<bookmarks->no_of_strings;i++)
  824.             {
  825.             if (bookmarks->extra_byte_flag != 0)
  826.                 {
  827.                 bookmarks->extra_bytes[i] = malloc(bookmarks->extra_byte_flag * sizeof(U8));
  828.                 if (bookmarks->extra_bytes[i] == NULL)
  829.                     {
  830.                     fprintf(erroroutput,"mem problem in bookmarks\n");
  831.                     exit(-1);
  832.                     }
  833.                 }
  834.             else
  835.                 bookmarks->extra_bytes[i]=NULL;
  836.  
  837.             if (bookmarks->exflag)
  838.                 {
  839.                 charlen = read_16ubit(tablefd);
  840.                 error(erroroutput,"got to here 1, charlen is %d\n",charlen);
  841.                 bookmarks->chars[i] = (U16*) malloc((charlen +1) * sizeof(U16));
  842.  
  843.                 if (bookmarks->chars[i] == NULL)
  844.                     {
  845.                     fprintf(erroroutput,"mem problem in bookmarks\n");
  846.                     exit(-1);
  847.                     }
  848.  
  849.                 for (j=0;j<charlen;j++)
  850.                     bookmarks->chars[i][j] = read_16ubit(tablefd);
  851.                 }
  852.             else
  853.                 {
  854.                 error(erroroutput,"got to here 2\n");
  855.                 charlen = getc(tablefd);
  856.                 bookmarks->chars[i] = (U16*) malloc((charlen +1) * sizeof(U16));
  857.  
  858.                 if (bookmarks->chars[i] == NULL)
  859.                     {
  860.                     fprintf(erroroutput,"mem problem in bookmarks\n");
  861.                     exit(-1);
  862.                     }
  863.                 for (j=0;j<charlen;j++)
  864.                     bookmarks->chars[i][j] = getc(tablefd);
  865.                 }
  866.  
  867.             bookmarks->chars[i][j] = '\0';
  868.             for(j=0;j<bookmarks->extra_byte_flag;j++)
  869.                 bookmarks->extra_bytes[i][j] = getc(tablefd);
  870.             }
  871.         }
  872.     }
  873.  
  874. void extract_bookm_limits(bookmark_limits *l_bookmarks,FILE *tablefd,U32 fcPlcfbkf,U32 lcbPlcfbkf, U32 fcPlcfbkl,U32 lcbPlcfbkl)
  875.     {
  876.     int i;
  877.     l_bookmarks->bookmark_b_no=0;
  878.     l_bookmarks->bookmark_b_cps=NULL;
  879.     l_bookmarks->bookmark_b_bkfs=NULL;
  880.     l_bookmarks->bookmark_e_no=0;
  881.     l_bookmarks->bookmark_e_cps=NULL;
  882.     if (lcbPlcfbkf > 0)
  883.         {
  884.         l_bookmarks->bookmark_b_no = (lcbPlcfbkf-4)/8;
  885.         l_bookmarks->bookmark_b_cps = (U32 *) malloc ((l_bookmarks->bookmark_b_no+1) * sizeof(U32));
  886.         l_bookmarks->bookmark_b_bkfs = (BKF *) malloc (l_bookmarks->bookmark_b_no * sizeof(BKF));
  887.  
  888.         if ((l_bookmarks->bookmark_b_bkfs==NULL) || (l_bookmarks->bookmark_b_cps ==NULL))
  889.             {
  890.             fprintf(erroroutput,"arrch, no mem for bookmarks\n");
  891.             exit(-1);
  892.             }
  893.  
  894.         fseek(tablefd,fcPlcfbkf,SEEK_SET);
  895.         for (i=0;i<l_bookmarks->bookmark_b_no+1;i++)
  896.             {
  897.             l_bookmarks->bookmark_b_cps[i] = read_32ubit(tablefd);
  898.             error(erroroutput,"the bookmark b cp is %x\n",l_bookmarks->bookmark_b_cps[i]);
  899.             }
  900.         for (i=0;i<l_bookmarks->bookmark_b_no;i++)
  901.             {
  902.             l_bookmarks->bookmark_b_bkfs[i].ibkl = read_16ubit(tablefd);
  903.             l_bookmarks->bookmark_b_bkfs[i].flags = read_16ubit(tablefd);
  904.             }
  905.         }
  906.  
  907.     if (lcbPlcfbkl> 0)
  908.         {
  909.         l_bookmarks->bookmark_e_no = (lcbPlcfbkl-4)/4;
  910.         l_bookmarks->bookmark_e_cps = (U32 *) malloc ((l_bookmarks->bookmark_e_no+1) * sizeof(U32));
  911.  
  912.         if (l_bookmarks->bookmark_e_cps ==NULL)
  913.             {
  914.             fprintf(erroroutput,"arrch, no mem for bookmarks\n");
  915.             exit(-1);
  916.             }
  917.  
  918.         fseek(tablefd,fcPlcfbkl,SEEK_SET);
  919.         for (i=0;i<l_bookmarks->bookmark_e_no+1;i++)
  920.             {
  921.             l_bookmarks->bookmark_e_cps[i] = read_32ubit(tablefd);
  922.             error(erroroutput,"the bookmark e cp is %x\n",l_bookmarks->bookmark_e_cps[i]);
  923.             }
  924.         }
  925.     }
  926.  
  927.  
  928.  
  929. /*this finds the beginning of a bookmark given a particular cp, adds
  930. the beginning tag and returns the next bookmark cp*/
  931. U32 decode_b_bookmark(bookmark_limits *l_bookmarks, STTBF *bookmarks)
  932.     {
  933.     int i=0;
  934.     U16 *letter;
  935.     while (i<l_bookmarks->bookmark_b_no)
  936.         {
  937.         if (l_bookmarks->bookmark_b_cps[i] == realcp)
  938.             {
  939.             l_bookmarks->bookmark_b_cps[i] = 0xffff;        /*mark it off the list*/
  940.             fprintf(outputfile,"<a name=\"");
  941.             letter = bookmarks->chars[i];
  942.             while (*letter != '\0')
  943.                 fprintf(outputfile,"%c",*letter++);
  944.             fprintf(outputfile,"\">");
  945.             if (i == l_bookmarks->bookmark_b_no-1)
  946.                 return(-1);
  947.             else
  948.                 return(l_bookmarks->bookmark_b_cps[i+1]);
  949.             }
  950.         i++;
  951.         }
  952.  
  953.     i=0;
  954.     while (i<l_bookmarks->bookmark_b_no)
  955.         {
  956.         if ( (l_bookmarks->bookmark_b_cps[i] != 0xffff) && (l_bookmarks->bookmark_b_cps[i] > realcp))
  957.             return(l_bookmarks->bookmark_b_cps[i]);
  958.         i++;
  959.         }
  960.  
  961.     if ((l_bookmarks->bookmark_b_no) > 0)
  962.         return(l_bookmarks->bookmark_b_cps[0]);
  963.         
  964.     return(-1);
  965.     }
  966.  
  967. U32 decode_e_bookmark(bookmark_limits *l_bookmarks)
  968.     {
  969.     int i=0;
  970.     
  971.     while (i<l_bookmarks->bookmark_e_no)
  972.         {
  973.         if (l_bookmarks->bookmark_e_cps[i] == realcp)
  974.             {
  975.             l_bookmarks->bookmark_e_cps[i] = 0xffff;        /*mark it off the list*/
  976.             fprintf(outputfile,"</A>");
  977.             if (i == l_bookmarks->bookmark_e_no-1)
  978.                 return(-1);
  979.             else
  980.                 return(l_bookmarks->bookmark_e_cps[i+1]);
  981.             }
  982.         i++;
  983.         }
  984.  
  985.     i=0;
  986.     while (i<l_bookmarks->bookmark_e_no)
  987.         {
  988.         if ((l_bookmarks->bookmark_e_cps[i] != 0xffff) && (l_bookmarks->bookmark_e_cps[i] > realcp))
  989.             return(l_bookmarks->bookmark_e_cps[i]);
  990.         i++;
  991.         }
  992.         
  993.     if ((l_bookmarks->bookmark_e_no) > 0)
  994.         return(l_bookmarks->bookmark_e_cps[0]);
  995.         
  996.     return(-1);
  997.     }
  998.